package com.mysql.mybatis.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mysql.mybatis.dao.mapper.UdalUserMapper;
import com.mysql.mybatis.dao.mapper.UserMapper;
import com.mysql.mybatis.dao.model.User;
import com.mysql.mybatis.dao.model.UserExample;
import com.mysql.mybatis.service.UserControllerService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : lhb
 * @date : 2019/11/5 10:55
 * @description: 用户服务实现
 */
@Service
public class UserControllerServiceImpl implements UserControllerService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UdalUserMapper udalUserMapper;

    @Override
    public List<User> getAllUsers() {
        UserExample example = new UserExample();
        return userMapper.selectByExample(example);
    }

    @Override
    public List<User> getByRowBounds(Integer pageNum, Integer pageSize) {
        RowBounds rowBounds = new RowBounds((pageNum - 1) * pageSize, pageSize);
        UserExample example = new UserExample();
        return userMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    @Override
    public List<User> getByPageHelper(Integer pageNum, Integer pageSize) {
        Page pages = PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.selectByExample(new UserExample());
        System.out.println("》》》》》总页数：" + pages.getTotal());
        return userList;
    }

    @Override
    public void save(User user) {
        Long seq = udalUserMapper.selectSeqByName("SEQ_TEST");
        user.setId(seq);
        userMapper.insertSelective(user);
    }

    @Override
    public User getById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public void deleteById(Long id) {
        userMapper.deleteByPrimaryKey(id);
    }


}